package com.jiai.cloud.api;

import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.entity.TGoodsSpac;
import com.jiai.cloud.feign.GoodsSpacAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * Created with j360-cloud-all -> me.j360.cloud.eurekaclient.api.
 * User: min_xu
 * Date: 2015/10/9
 * Time: 10:53
 * 说明：
 */

@RestController
@RequestMapping("/api/v1/goodsSpac")

public class APIGoodsSpacController {


    @Autowired
    GoodsSpacAPI goodsSpacAPI;


    /**
     * 直接调用feign，feign会去调用eurekaService
     * */

    //增
    @RequestMapping(value = "/addgoodsSpac", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object saveGoodsSpac (@RequestBody TGoodsSpac tGoodsSpac) throws ServiceException {
        try {
            goodsSpacAPI.addGoodsSpac(tGoodsSpac);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("插入失敗", e.getMessage());
        }
    }

    //删
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object deleteById (@PathVariable("id") Integer id) throws ServiceException {
        try {
            goodsSpacAPI.deleteById(id);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }

    //查询所有的
    @RequestMapping(value = "/findByAll/{page}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object findGoodsSpacByAll(@PathVariable("page") Integer page1) throws ServiceException {
        try{
            return RestResultGenerator.genSuccessResult(goodsSpacAPI.selectPage(page1));
        }catch (Exception e){
            throw new ServiceException("chaxun",e.getMessage());
        }

    }
    //通过ID查询
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object findById(@PathVariable("id") Integer id) throws ServiceException {
        try{
            goodsSpacAPI.selectById(id);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }


//    改(有就改，没有就插入)
    @RequestMapping(value = "/edit", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object edit (@RequestBody TGoodsSpac tGoodsSpac)throws ServiceException {
        try {
            goodsSpacAPI.edit(tGoodsSpac);
            return RestResultGenerator.genSuccessResult() ;
        } catch (Exception e) {
            throw new ServiceException("编辑失敗", e.getMessage());

        }
    }









}